package com.weitu.contractor.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.contractor.entity.Trainee;
import com.weitu.contractor.entity.TraineeCriteria;
import com.weitu.contractor.service.TraineeService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * @author LiMeng 2017/4/28
 */
@Controller
@RequestMapping(value = "contractor/trainee")
public class TraineeController extends BaseController {
    private final TraineeService traineeService;

    @Autowired
    public TraineeController(TraineeService traineeService) {
        this.traineeService = traineeService;
    }


    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request, String sort, String trainId) throws Exception {
        TraineeCriteria criteria = new TraineeCriteria();
        criteria.createCriteria()
                .andDelStateEqualTo(0)
                .andTrainIdEqualTo(trainId);

        int count = traineeService.countByCriteria(criteria);

        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, count);//获取分页对象
        PageView pageView = dataGridPagination.getPageView();//生成pageView分页对象
        //未传sort参数是采用添加时间排序
        if (StringUtils.isEmpty(sort)) {
            pageView.setSortColumn("addTime");
        } else {
            criteria.setOrderByClause(pageView.getSortColumn() + " " + pageView.getOrderType());
        }
        criteria.setPageView(pageView);

        List<Trainee> trainees = traineeService.selectByCriteria(criteria);

        return dataGridPagination.flushDataGridData(trainees);
    }

    @SystemLog(module = "施工人员培训", methods = "更新人员信息")
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult updateAjax(Trainee trainee) throws Exception {
        trainee.setUpdateUserId(getSessionUser().getId());
        trainee.setUpdateDeptId(getSessionUser().getDepartment().getId());
        trainee.setUpdateTime(DateTimeUtil.formatterDateTime());
        if (traineeService.updateByPrimaryKeySelective(trainee) > 0) {
            return success("保存成功！");
        }
        return fail("保存失败！");
    }

    @SystemLog(module = "施工人员培训", methods = "添加人员信息")
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult saveAjax(String id, String trainId) throws Exception {
        if (StringUtils.isEmpty(trainId)) {
            return fail("添加失败！");
        }
        Trainee trainee = new Trainee();
        trainee.setId(id);
        trainee.setAddUserId(getSessionUser().getId());
        trainee.setAddDeptId(getSessionUser().getDepartment().getId());
        trainee.setAddTime(DateTimeUtil.formatterDateTime());
        trainee.setTrainId(trainId);
        if (traineeService.insertSelective(trainee) > 0) {
            return success("添加成功！");
        }
        return fail("添加失败！");
    }

    @SystemLog(module = "施工人员培训", methods = "删除人员信息")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxResult deleteAjax(String id) throws Exception {
        if (traineeService.deleteByPrimaryKey(id) > 0) {
            return success("删除成功！");
        }
        return fail("删除失败！");
    }
}
